import { Message } from 'element-ui'

//是否为NULL
export function isNull (value) {
    if (value == null || typeof (value) == 'undefined' || value === undefined || value === '') {
        return true;
    }
    return false;
}


//是否为空(包括空字符串、空格、null,{})
export function isEmpty (strings) {
    if (!isNull(strings)) { // 先判断是否为null，返回true ，判断是否为空字符串，返回true
        // 空对象
        if (strings instanceof Object) {
            if (JSON.stringify(strings) == "{}") {
                return true
            }
            43
        }
        if ((strings + '').replace(/(^\s*)|(\s*$)/g, '').length === 0) { //已修正bug[当strings为数字时，会报strings.replace is not a function]
            return true;
        }
    } else {
        return true;
    }
    // 不为空返回false
    return false;
}

/**
 * 是否为数字
 */
export const isIntNum = function (val) {
    var regPos = /^\d+(\.\d+)?$/; //非负浮点数
    var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
    if (regPos.test(val) || regNeg.test(val)) {
        return true;
    } else {
        return false;
    }
}


//list转json
export function listToJson (params) {
    var param_str = "";
    for (var i = 0; i < params.length; i++) {
        //循环LIST
        if (i == params.length - 1) {
            param_str += '"' + params[i].name + '":"' + params[i].value + '"';
        } else {
            param_str += '"' + params[i].name + '":"' + params[i].value + '",';
        }
    }
    if (param_str != "") {
        param_str = "{" + param_str + "}";
    }
    return param_str;
}

//判断字符串是否为JSON格式
export function isJSON (str) {
    if (typeof str == 'string') {
        try {
            var obj = JSON.parse(str);
            if (typeof obj == 'object' && obj) {
                return true;
            } else {
                return false;
            }
        } catch (e) {
            console.log('error：' + str + '!!!' + e);
            return false;
        }
    }
}

//求百分比
export function getPercent (num, total) {
    num = parseFloat(num);
    total = parseFloat(total);
    if (isNaN(num) || isNaN(total)) {
        return "-";
    }
    return total <= 0 ? "0%" : (Math.round(num / total * 10000) / 100.00) + "%";
}


//js判断是否是object
export function isObj (object) {// 判断是否是object
    return object && typeof (object) == 'object' && Object.prototype.toString.call(object).toLowerCase() == "[object object]";
}

//js判断是否是array
export function isArray (object) {// 判断是否是array
    return Object.prototype.toString.call(o) === '[object Array]';
}


export function dragControllerDiv () {
    var resize = document.getElementsByClassName('resize');
    var left = document.getElementsByClassName('left');
    var mid = document.getElementsByClassName('mid');
    var box = document.getElementsByClassName('drop-box');
    for (let i = 0; i < resize.length; i++) {
        // 鼠标按下事件
        resize[i].onmousedown = function (e) {
            //颜色改变提醒
            resize[i].style.background = '#818181';
            var startX = e.clientX;
            resize[i].left = resize[i].offsetLeft;
            // 鼠标拖动事件
            document.onmousemove = function (e) {
                var endX = e.clientX;
                var moveLen = resize[i].left + (endX - startX); // （endx-startx）=移动的距离。resize[i].left+移动的距离=左边区域最后的宽度
                var maxT = box[i].clientWidth - resize[i].offsetWidth; // 容器宽度 - 左边区域的宽度 = 右边区域的宽度

                if (moveLen < 32) moveLen = 32; // 左边区域的最小宽度为32px
                if (moveLen > maxT - 150) moveLen = maxT - 150; //右边区域最小宽度为150px

                resize[i].style.left = moveLen; // 设置左侧区域的宽度

                for (let j = 0; j < left.length; j++) {
                    left[j].style.width = moveLen + 'px';
                    mid[j].style.width = (box[i].clientWidth - moveLen - 10) + 'px';
                }
            };
            // 鼠标松开事件
            document.onmouseup = function (evt) {
                //颜色恢复
                resize[i].style.background = '#a7a7a7';
                document.onmousemove = null;
                document.onmouseup = null;
                resize[i].releaseCapture && resize[i].releaseCapture(); //当你不在需要继续获得鼠标消息就要应该调用ReleaseCapture()释放掉
            };
            resize[i].setCapture && resize[i].setCapture(); //该函数在属于当前线程的指定窗口里设置鼠标捕获
            return false;
        };
    }
}


//复制功能
export function copy (data) {
    let oInput = document.createElement('textarea'); //js创建一个input输入框,input不会换行，所以用textarea
    oInput.value = data; //将需要复制的文本赋值到创建的input输入框中
    document.body.appendChild(oInput); // 将输入框暂时创建到实例里面
    oInput.select(); // 选择输入框内容
    document.execCommand("Copy"); // 执行浏览器复制命令
    Message({
        message: '复制成功',
        type: 'success'
    });
    oInput.remove() // 最后删除实例中临时创建的input输入框，完成复制操作
}


// 获得某一周的开始日期
export function getWeekStartDate (nowYear, nowMonth, nowDay, nowDayOfWeek) {
    let weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek)
    return formatDate(weekStartDate)

}

// 获得某一周的结束日期
export function getWeekEndDate (nowYear, nowMonth, nowDay, nowDayOfWeek) {
    let weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek))
    return formatDate(weekEndDate)

}

// 日期格式化
export function formatDate (date) {

    var myyear = date.getYear();
    var mymonth = date.getMonth() + 1;
    var myweekday = date.getDate();
    if (mymonth < 10) {
        mymonth = "0" + mymonth;
    }
    if (myweekday < 10) {
        myweekday = "0" + myweekday;
    }
    return mymonth + "-" + myweekday

}

